Imports System.Data

Class frm<OwnerU>_<TableU>

    Private cls<Database>Data As New <Database>DataClass
    Private cls<OwnerU>_<TableU>Data As New <OwnerU>_<TableU>DataClass
    Private cls<DOwnerU>_<DTableU>Data As New <DOwnerU>_<DTableU>DataClass

    Private AddMode As Boolean = False
    Private EditMode As Boolean = False
    Private DeleteMode As Boolean = False
    Private Row As Int32 = 0

    Public <DOwnerU>_<DTableU> As New frm<DOwnerU>_<DTableU>

    Private Sub frm<OwnerU>_<TableU>_Loaded(sender As Object, e As System.Windows.RoutedEventArgs) Handles Me.Loaded
        cmbFields.Items.Clear()
    <If Loop>
   	<IsNotText>
   	<And>
   	<IsNotNText>
        cmbFields.Items.Add("<FieldC>")
    </If Loop>

        cmbCondition.Items.Clear()
        cmbCondition.Items.Add("Contains")
        cmbCondition.Items.Add("Equals")
        cmbCondition.Items.Add("Starts with...")
        cmbCondition.Items.Add("More than...")
        cmbCondition.Items.Add("Less than...")
        cmbCondition.Items.Add("Equal or more than...")
        cmbCondition.Items.Add("Equal or less than...")

        InitToolBarItems()
        tiGrid.Height = 0
        tiDetail.Height = 0
        <If Loop>
          <IsFKey>
        Load<OwnerU>_<TableU>_<ROwnerU>_<RTableU>ComboBox<MiscTag>()
        </If Loop>
        LoadGrid(True)
        tiGrid.IsSelected = True

        Detail.Children.Add(<DOwnerU>_<DTableU>)
    End Sub

    <If Loop>
      <IsFKey>
    Private Sub Load<OwnerU>_<TableU>_<ROwnerU>_<RTableU>ComboBox<MiscTag>()
        Using New WaitCursor()
                <IsFKey>
            cb<FieldU>.ItemsSource = <OwnerU>_<TableU>_<ROwnerU>_<RTableU>DataClass<MiscTag>.Select<ROwnerU>_<RTableU>
            cb<FieldU>.SelectedValuePath = "<RFieldU>"
            cb<FieldU>.DisplayMemberPath = "<RDisplayFieldU>"
        End Using
    End Sub

    </If Loop>
    Private Sub LoadGrid(ByVal SelectRow As Boolean)
        Using New WaitCursor()
            Try
                Dim dt As DataTable = cls<OwnerU>_<TableU>Data.SelectAll()
                With Grid
                    .ItemsSource = dt.DefaultView
                    .CanUserAddRows = False
                    .CanUserDeleteRows = False
                    .IsReadOnly = True
                    If .Items.Count > 0 And SelectRow = True Then
                        .SelectedItem = .Items(0)
                        .ScrollIntoView(.SelectedItem)
                    End If
                End With
            Catch ex As System.Exception
                MsgBox("An error occurred in LoadGrid()..." & vbCrLf & ex.Message)
            Finally
            End Try
        End Using
    End Sub

    Private Sub Grid_AutoGeneratingColumn(sender As Object, e As System.Windows.Controls.DataGridAutoGeneratingColumnEventArgs) Handles Grid.AutoGeneratingColumn
        Dim headername As String = e.Column.Header.ToString()
        Dim txtCol As DataGridTextColumn = TryCast(e.Column, DataGridTextColumn)
    <If Loop>
        <IsDateTimePicker>
        If headername = "<FieldC>" Then
            txtCol.Binding.StringFormat = "{0:d}"
        End If
        <IsMoney>
        If headername = "<FieldC>" Then
            txtCol.Binding.StringFormat = "{0:C2}"
            txtCol.ElementStyle = CType(Grid.FindResource("rightAlignment"), Windows.Style)
        End If
        <IsSmallMoney>
        If headername = "<FieldC>" Then
            txtCol.Binding.StringFormat = "{0:C2}"
            txtCol.ElementStyle = CType(Grid.FindResource("rightAlignment"), Windows.Style)
        End If
        <IsNumber>
        <And>
        <IsNotFKey>
        <And>
        <IsNotMoney>
        <And>
        <IsNotSmallMoney>
        <And>
        <IsNotPKey>
        <And>
        <IsNotIdentity>
        If headername = "<FieldC>" Then
            txtCol.Binding.StringFormat = "{0:N<DecimalPlaces>}"
            txtCol.ElementStyle = CType(Grid.FindResource("rightAlignment"), Windows.Style)
        End If
    </If Loop>
    End Sub

    Private Sub tbiAdd_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiAdd.Click
        Add()
        tiDetail.IsSelected = True

        InitToolBarItems()
        tbiAdd.IsEnabled = False
        tbiEdit.IsEnabled = False
        tbiDelete.IsEnabled = False
        tbiApply.IsEnabled = True
        tbiCancel.IsEnabled = True
        tbiRefresh.IsEnabled = False
    End Sub

    Private Sub tbiEdit_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiEdit.Click
        Edit()
        tiDetail.IsSelected = True

        InitToolBarItems()
        tbiAdd.IsEnabled = False
        tbiEdit.IsEnabled = False
        tbiDelete.IsEnabled = False
        tbiApply.IsEnabled = True
        tbiCancel.IsEnabled = True
        tbiRefresh.IsEnabled = False
    End Sub

    Private Sub tbiDelete_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiDelete.Click
        Delete()
        tiDetail.IsSelected = True

        InitToolBarItems()
        tbiAdd.IsEnabled = False
        tbiEdit.IsEnabled = False
        tbiDelete.IsEnabled = False
        tbiApply.IsEnabled = True
        tbiCancel.IsEnabled = True
        tbiRefresh.IsEnabled = False
    End Sub

    Private Sub tbiApply_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiApply.Click
        If Me.AddMode = True Then
            Me.InsertRecord()
        ElseIf Me.EditMode = True Then
            Me.UpdateRecord()
        ElseIf Me.DeleteMode = True Then
            Me.DeleteRecord()
        End If
    End Sub

    Private Sub tbiCancel_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiCancel.Click
        tiGrid.IsSelected = True
        InitToolBarItems()
    End Sub

    Private Sub tbiRefresh_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiRefresh.Click
        LoadGrid(True)
    End Sub

    Private Sub tbiClose_Click(sender As Object, e As System.Windows.RoutedEventArgs) Handles tbiClose.Click
        Me.Close()
    End Sub

    Private Sub Grid_SelectionChanged(sender As Object, e As System.Windows.Controls.SelectionChangedEventArgs) Handles Grid.SelectionChanged
        Row = Grid.SelectedIndex
    End Sub

    Private Sub InitToolBarItems()
        tbiAdd.IsEnabled = True
        tbiEdit.IsEnabled = True
        tbiDelete.IsEnabled = True
        tbiApply.IsEnabled = False
        tbiCancel.IsEnabled = False
        tbiRefresh.IsEnabled = True
        tbiClose.IsEnabled = True
    End Sub

    Private Sub GetData()
        Using New WaitCursor()
            ClearRecord()

            Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>
        <If Loop>
           <IsPKey>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
           <ExitLoop>
        </If Loop>
            cls<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Record(cls<OwnerU>_<TableU>)

            If Not cls<OwnerU>_<TableU> Is Nothing Then
                Try
                <If Loop>
                       <IsCheckBox>
                       <And>
                       <IsNull>
                    cb<FieldU>.IsChecked = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, <Convert>(cls<OwnerU>_<TableU>.<Field>))
                       <IsNumericUpDown>
                       <And>
                       <IsNull>
                    nud<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, <Convert>(cls<OwnerU>_<TableU>.<Field>).ToString)
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNull>
                    dtp<FieldU>.SelectedDate = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Date.Now, System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<Field>))
                       <IsDateTimePicker>
 	               <And>
                       <IsDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNull>
                    dtp<FieldU>.SelectedDate = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Date.Now, <Convert>(cls<OwnerU>_<TableU>.<Field>.DateTime))
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsTime>
                       <And>
                       <IsNull>
                    dtp<FieldU>.SelectedDate = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Date.Now, Convert.ToDateTime(cls<OwnerU>_<TableU>.<Field>.ToString))
                       <IsTextBox>
                       <And>
                       <IsNull>
                    tb<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                       <IsMaskedTextBox>
                       <And>
                       <IsNull>
                    mtb<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                       <IsMultilineTextBox>
                       <And>
                       <IsNull>
                    mtb<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                       <IsRichTextBox>
                       <And>
                       <IsNull>
                    rtb<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                       <IsPictureBox>
                       <And>
                       <IsNull>
                    pb<FieldU>.Value = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, <Convert>(cls<OwnerU>_<TableU>.<Field>))
                       <IsComboBox>
                       <And>
                       <IsNull>
                    cb<FieldU>.Text = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, Convert.ToString(cls<OwnerU>_<TableU>.<Field>))
                       <IsComboBox (Db)>
                       <And>
                       <IsNull>
                    cb<FieldU>.SelectedValue = If(cls<OwnerU>_<TableU>.<Field> Is Nothing, Nothing, <Convert>(cls<OwnerU>_<TableU>.<Field>))
--------------------------------------------------------------------------------------------------------<Comment>
                       <IsCheckBox>
                       <And>
                       <IsNotNull>
                    cb<FieldU>.IsChecked = <Convert>(cls<OwnerU>_<TableU>.<Field>)
                       <IsNumericUpDown>
                       <And>
                       <IsNotNull>
                    nud<FieldU>.Text = <Convert>(cls<OwnerU>_<TableU>.<Field>).ToString
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNotNull>
                    dtp<FieldU>.SelectedDate = System.Convert.ToDateTime(cls<OwnerU>_<TableU>.<Field>)
                       <IsDateTimePicker>
 	               <And>
                       <IsDateTimeOffset>
 	               <And>
                       <IsNotTime>
                       <And>
                       <IsNotNull>
                    dtp<FieldU>.SelectedDate = <Convert>(cls<OwnerU>_<TableU>.<Field>.DateTime)
                       <IsDateTimePicker>
 	               <And>
                       <IsNotDateTimeOffset>
 	               <And>
                       <IsTime>
                       <And>
                       <IsNotNull>
                    dtp<FieldU>.SelectedDate = Convert.ToDateTime(cls<OwnerU>_<TableU>.<Field>.ToString)
                       <IsTextBox>
                       <And>
                       <IsNotNull>
                    tb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                       <IsMaskedTextBox>
                       <And>
                       <IsNotNull>
                    mtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                       <IsMultilineTextBox>
                       <And>
                       <IsNotNull>
                    mtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                       <IsRichTextBox>
                       <And>
                       <IsNotNull>
                    rtb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                       <IsPictureBox>
                       <And>
                       <IsNotNull>
                    pb<FieldU>.Value = <Convert>(cls<OwnerU>_<TableU>.<Field>)
                       <IsComboBox>
                       <And>
                       <IsNotNull>
                    cb<FieldU>.Text = Convert.ToString(cls<OwnerU>_<TableU>.<Field>)
                       <IsComboBox (Db)>
                       <And>
                       <IsNotNull>
                    cb<FieldU>.SelectedValue = <Convert>(cls<OwnerU>_<TableU>.<Field>)
                </If Loop>
                Catch
                End Try
            End If

            If Not cls<OwnerU>_<TableU> Is Nothing Then
                Try
                    <DOwnerU>_<DTableU>.<DRField> = cls<OwnerU>_<TableU>.<Field>
                    <DOwnerU>_<DTableU>.LoadGrid(True)
                Catch
                End Try
            End If
        End Using
    End Sub

    Private Sub SetData(ByVal cls<OwnerU>_<TableU> As <OwnerU>_<TableU>)
        Using New WaitCursor()
            With cls<OwnerU>_<TableU>
                <If Loop>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(CBool(cb<FieldU>.IsChecked), True, False)
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(nud<FieldU>.Text), Nothing, CType(nud<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(dtp<FieldU>.Text), Nothing, CType(dtp<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(tb<FieldU>.Text), Nothing, CType(tb<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(mtb<FieldU>.Text), Nothing, CType(mtb<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(mtb<FieldU>.Text), Nothing, CType(mtb<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(rtb<FieldU>.Text), Nothing, CType(rtb<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                          
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = <Convert>(cb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNull>
                    <And>
                    <IsNullable>
                .<FieldU> = If(String.IsNullOrEmpty(cb<FieldU>.SelectedValue.ToString), Nothing, CType(cb<FieldU>.SelectedValue, <VBType>?))
--------------------------------------------------------------------------------------------------------<Comment>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(CBool(cb<FieldU>.IsChecked), True, False)
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(nud<FieldU>.Text), Nothing, CType(nud<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(dtp<FieldU>.Text), Nothing, CType(dtp<FieldU>.Text, <VBType>?))
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(tb<FieldU>.Text), Nothing, tb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(mtb<FieldU>.Text), Nothing, mtb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(mtb<FieldU>.Text), Nothing, mtb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(rtb<FieldU>.Text), Nothing, rtb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                          
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = <Convert>(cb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNull>
                    <And>
                    <IsNotNullable>
                .<FieldU> = If(String.IsNullOrEmpty(cb<FieldU>.SelectedValue.ToString), Nothing, cb<FieldU>.SelectedValue.ToString)
--------------------------------------------------------------------------------------------------------<Comment>
                    <IsNotIdentity>
                    <And>
                    <IsCheckBox>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(IIf(CBool(cb<FieldU>.IsChecked), True, False))
                    <IsNotIdentity>
                    <And>
                    <IsNumericUpDown>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(nud<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsDateTimePicker>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(dtp<FieldU>.SelectedDate)
                    <IsNotIdentity>
                    <And>
                    <IsTextBox>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(tb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMaskedTextBox>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(mtb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsMultilineTextBox>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(mtb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsRichTextBox>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(rtb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsPictureBox>
                    <And>
                    <IsNotNull>
                          
                    <IsNotIdentity>
                    <And>
                    <IsComboBox>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(cb<FieldU>.Text)
                    <IsNotIdentity>
                    <And>
                    <IsComboBox (Db)>
                    <And>
                    <IsNotNull>
                .<FieldU> = <Convert>(cb<FieldU>.SelectedValue)
--------------------------------------------------------------------------------------------------------<Comment>
                </If Loop>
            End With
        End Using
    End Sub

    Private Sub Add()
        Me.AddMode = True
        Me.EditMode = False
        Me.DeleteMode = False

        ClearRecord()
        IsReadOnlyRecord(True)
 -----------------------------------------------------<Comment>
        <If Loop>
           <IsUniqueIdentifier>
           <And>
           <IsTextBox>
        Me.tb<FieldU>.Text = Guid.NewGuid.ToString
           <IsUniqueIdentifier>
           <And>
           <IsMaskedTextBox>
        Me.tb<FieldU>.Text = Guid.NewGuid.ToString
        </If Loop>
 -----------------------------------------------------<Comment>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsCheckBox>
        cb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNumericUpDown> 
        nud<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsDateTimePicker>
        dtp<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsTextBox>
        tb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsMaskedTextBox>
        mtb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsMultilineTextBox>
        mtb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsRichTextBox>
        rtb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPictureBox>
        pb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsComboBox>
        cb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsComboBox (Db)>
        cb<FieldU>.Focus()
              <ExitLoop>
        </If Loop>
-----------------------------------------------------<Comment>

        <DOwnerU>_<DTableU>.LoadGrid(False)
        pnlDetail.Visibility = System.Windows.Visibility.Collapsed
    End Sub

    Private Sub Edit()
        Me.AddMode = False
        Me.EditMode = True
        Me.DeleteMode = False

        GetData()

        IsReadOnlyRecord(True)
-----------------------------------------------------<Comment>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
        nud<FieldU>.IsEnabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
        tb<FieldU>.IsEnabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
        mtb<FieldU>.IsEnabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
        mtb<FieldU>.IsEnabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
        rtb<FieldU>.IsEnabled = False
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
        cb<FieldU>.IsEnabled = False
           <IsNotIdentity>
           <And>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
        nud<FieldU>.IsEnabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
        tb<FieldU>.IsEnabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
        mtb<FieldU>.IsEnabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
        mtb<FieldU>.IsEnabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
        rtb<FieldU>.IsEnabled = False
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
        cb<FieldU>.IsEnabled = False
           <ExitLoop>
        </If Loop>
-----------------------------------------------------<Comment>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsCheckBox>
        cb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsNumericUpDown> 
        nud<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsDateTimePicker>
        dtp<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsTextBox>
        tb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsMaskedTextBox>
        mtb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsMultilineTextBox>
        mtb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsRichTextBox>
        rtb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsPictureBox>
        pb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsComboBox>
        cb<FieldU>.Focus()
              <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsNotPKey>
           <And>
           <IsComboBox (Db)>
        cb<FieldU>.Focus()
              <ExitLoop>
        </If Loop>

        pnlDetail.Visibility = System.Windows.Visibility.Visible
-----------------------------------------------------<Comment>
    End Sub

    Private Sub Delete()
        Me.AddMode = False
        Me.EditMode = False
        Me.DeleteMode = True

        GetData()

        IsReadOnlyRecord(False)

        pnlDetail.Visibility = System.Windows.Visibility.Collapsed
    End Sub

    Private Sub IsReadOnlyRecord(ByVal YesNo As Boolean)
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsCheckBox>
        Me.cb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsNumericUpDown>
        Me.nud<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsDateTimePicker>
        Me.dtp<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsTextBox>
        Me.tb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsMaskedTextBox>
        Me.mtb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsMultilineTextBox>
        Me.mtb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsRichTextBox>
        Me.rtb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsPictureBox>
        Me.pb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsComboBox>
        Me.cb<FieldU>.IsEnabled = YesNo
           <IsNotIdentity>
           <And>
           <IsComboBox (Db)>
        Me.cb<FieldU>.IsEnabled = YesNo
        </If Loop>
    End Sub

    Private Sub ClearRecord()
        <If Loop>
           <IsCheckBox>
        Me.cb<FieldU>.IsChecked = False
           <IsNumericUpDown>
        Me.nud<FieldU>.Text = "0"
           <IsDateTimePicker>
        Me.dtp<FieldU>.Text = Nothing
           <IsTextBox>
        Me.tb<FieldU>.Text = Nothing
           <IsMaskedTextBox>
        Me.mtb<FieldU>.Text = Nothing
           <IsMultilineTextBox>
        Me.mtb<FieldU>.Text = Nothing
           <IsRichTextBox>
        Me.rtb<FieldU>.Text = Nothing
           <IsPictureBox>
        Me.pb<FieldU>.Text = Nothing
           <IsComboBox>
        Me.cb<FieldU>.SelectedIndex = -1
           <IsComboBox (Db)>
        Me.cb<FieldU>.SelectedIndex = -1
        </If Loop>
    End Sub

    Private Sub GoBack_To_Grid()
        Dim LastRow As Int32 = Row
        Dim gridOK As Boolean = False
        Try
            LoadGrid(False)
            With Grid
                .SelectedItems.Clear()
                .SelectedItem = .Items(LastRow)
                .ScrollIntoView(.Items(LastRow))
            End With
            tiGrid.IsSelected = True
            InitToolBarItems()
            gridOK = True
        Catch
            'Hide error message.
        Finally
            If gridOK = False Then
                LoadGrid(True)
                tiGrid.IsSelected = True
                InitToolBarItems()
            End If
        End Try
    End Sub

    Private Sub InsertRecord()
        Using New WaitCursor()
            Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>
            If VerifyData() = True Then
                SetData(cls<OwnerU>_<TableU>)
                Dim bSucess As Boolean
                bSucess = cls<OwnerU>_<TableU>Data.Add(cls<OwnerU>_<TableU>)
                If bSucess = True Then
                    GoBack_To_Grid()
                Else
                    MsgBox("Insert failed.", MsgBoxStyle.OkOnly, "Error")
                End If
            End If
        End Using
    End Sub

    Private Sub UpdateRecord()
        Using New WaitCursor()
            Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>
        <If Loop>
           <IsPKey>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
           <ExitLoop>
        </If Loop>
            cls<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Record(cls<OwnerU>_<TableU>)

            If VerifyData() = True Then
                SetData(cls<OwnerU>_<TableU>)
                Dim bSucess As Boolean
                bSucess = cls<OwnerU>_<TableU>Data.Update(cls<OwnerU>_<TableU>)
                If bSucess = True Then
                    GoBack_To_Grid()
                Else
                    MsgBox("Update failed.", MsgBoxStyle.OkOnly, "Error")
                End If
            End If
        End Using
    End Sub

    Private Sub DeleteRecord()
        Using New WaitCursor()
            Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>
        <If Loop>
           <IsPKey>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
        </If Loop>
        <If Loop>
           <IsNotPKey>
           <And>
           <IsIdentity>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
            cls<OwnerU>_<TableU>.<FieldU> = <Convert>(TryCast(Grid.SelectedCells(<ColNo>).Column.GetCellContent(Grid.SelectedItem), TextBlock).Text)
           <ExitLoop>
        </If Loop>
            If MessageBox.Show("Are you sure? Delete this record?", "Delete", MessageBoxButton.YesNo, MessageBoxImage.Question) = MessageBoxResult.Yes Then
                SetData(cls<OwnerU>_<TableU>)
                Dim bSucess As Boolean
                bSucess = cls<DOwnerU>_<DTableU>Data.Delete(<DOwnerU>_<DTableU>.<DRField>)
                bSucess = cls<OwnerU>_<TableU>Data.Delete(cls<OwnerU>_<TableU>)
                If bSucess = True Then
                    GoBack_To_Grid()
                Else
                    MsgBox("Delete failed.", MsgBoxStyle.OkOnly, "Error")
                End If
            End If
        End Using
    End Sub

    Private Function VerifyData() As Boolean
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsCheckBox>
          
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsNumericUpDown>
        If nud<FieldU>.Text = "" Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            nud<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsDateTimePicker>
        If dtp<FieldU>.Text = "" Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            dtp<FieldU>.Focus()
            Return False
        End If        
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsTextBox>
        If tb<FieldU>.Text = "" Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            tb<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsMaskedTextBox>
        If mtb<FieldU>.Text = "" Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            mtb<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsMultilineTextBox>
        If mtb<FieldU>.Text = "" Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            mtb<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsRichTextBox>
        If rtb<FieldU>.Text = "" Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            rtb<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsPictureBox>
        
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsComboBox>
        If cb<FieldU>.SelectedIndex = -1 Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            cb<FieldU>.Focus()
            Return False
        End If
           <IsNotIdentity>
           <And>
           <IsNotNull>
           <And>
           <IsComboBox (Db)>
        If cb<FieldU>.SelectedIndex = -1 Then
            MsgBox("<FieldC> is Required", MsgBoxStyle.OkOnly, "Entry Error")
            cb<FieldU>.Focus()
            Return False
        End If         
      </If Loop>
---------------------------------------<Comment>
        <If Loop>
           <IsNotIdentityCount>
        If _
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
            nud<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
            tb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
            mtb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
            mtb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
            rtb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
             cb<FieldU>.SelectedIndex <> -1 _
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
           <And>
           <IsCurRow<>[0]>
            And nud<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
           <And>
           <IsCurRow<>[0]>
            And tb<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
           <And>
           <IsCurRow<>[0]>
            And mtb<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
           <And>
           <IsCurRow<>[0]>
            And mtb<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
           <And>
           <IsCurRow<>[0]>
            And rtb<FieldU>.Text <> "" _
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
           <And>
           <IsCurRow<>[0]>
             And cb<FieldU>.SelectedIndex <> -1 _
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
             nud<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
             tb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
             mtb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
             mtb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
             rtb<FieldU>.Text <> "" _
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
             cb<FieldU>.SelectedIndex <> -1 _
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
             Then  
             Dim cls<OwnerU>_<TableU> As New <OwnerU>_<TableU>
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(tb<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(rtb<FieldU>.Text)
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue)
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(nud<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(tb<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(mtb<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(rtb<FieldU>.Text)
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
             cls<OwnerU>_<TableU>.<FieldU> = <Convert>(cb<FieldU>.SelectedValue)
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
             cls<OwnerU>_<TableU> = cls<OwnerU>_<TableU>Data.Select_Record(cls<OwnerU>_<TableU>)
             If Not cls<OwnerU>_<TableU> Is Nothing And Me.AddMode = True Then
                  MsgBox("Record already exists", MsgBoxStyle.OkOnly, "Entry Error")
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsNumericUpDown>
                  nud<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsTextBox>
                  tb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMaskedTextBox>
                  mtb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsMultilineTextBox>
                  mtb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsRichTextBox>
                  rtb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentity>
           <And>
           <IsPKey>
           <And>
           <IsComboBox (Db)>
                  cb<FieldU>.Focus()
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsNumericUpDown>
                  nud<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsTextBox>
                  tb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMaskedTextBox>
                  mtb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsMultilineTextBox>
                  mtb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsRichTextBox>
                  rtb<FieldU>.Focus()
           <ExitLoop>
           <IsNotIdentityCount>
           <And>
           <IsNotPKeyCount>
           <And>
           <IsComboBox (Db)>
                  cb<FieldU>.Focus()
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
                  Return False
             End If
           <ExitLoop>
        </If Loop>
        <If Loop>
           <IsNotIdentityCount>
        End If
           <ExitLoop>
        </If Loop>
---------------------------------------<Comment>
        Return True
    End Function

        <If Loop>
           <IsNumericUpDown>
    Private Sub nud<FieldU>_PreviewTextInput(sender As Object, e As System.Windows.Input.TextCompositionEventArgs) Handles nud<FieldU>.PreviewTextInput
        If Not Char.IsDigit(CChar(e.Text)) Then e.Handled = True
    End Sub

        </If Loop>

    Private Sub cbExport_SelectionChanged(sender As Object, e As SelectionChangedEventArgs) Handles cbExport.SelectionChanged
        Using New WaitCursor()
            Try
                Dim cls<Database>Data As New <Database>DataClass
                If cbiPDF.IsSelected = True Then
                    If tiGrid.IsSelected Then
                        cls<Database>Data.ExportToPdf("Many", _
                                                      "<Head>", Grid, CType(Grid.CurrentItem, DataRowView), Row)
                    ElseIf tiDetail.IsSelected Then
                        cls<Database>Data.ExportToPdf("One", _
                                                      "<Head>", Grid, CType(Grid.CurrentItem, DataRowView), Row)
                    End If
                ElseIf cbiExcel.IsSelected = True Then
                    If tiGrid.IsSelected Then
                        cls<Database>Data.ExportToExcel("Many", _
                                                      "<Head>", Grid, CType(Grid.CurrentItem, DataRowView), Row)
                    ElseIf tiDetail.IsSelected Then
                        cls<Database>Data.ExportToExcel("One", _
                                                      "<Head>", Grid, CType(Grid.CurrentItem, DataRowView), Row)
                    End If
                ElseIf cbiWord.IsSelected = True Then
                    If tiGrid.IsSelected Then
                        cls<Database>Data.ExportToWord("Many", _
                                                      "<Head>", Grid, CType(Grid.CurrentItem, DataRowView), Row)
                    ElseIf tiDetail.IsSelected Then
                        cls<Database>Data.ExportToWord("One", _
                                                      "<Head>", Grid, CType(Grid.CurrentItem, DataRowView), Row)
                    End If
                End If
                cbiExport.IsSelected = True
            Catch
            End Try
        End Using
    End Sub

    Private Sub butShowAll_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butShowAll.Click
        txtSearch.Text = Nothing
        LoadGrid(True)
        tiGrid.IsSelected = True
    End Sub

    Private Sub butSearch_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles butSearch.Click
        Using New WaitCursor()
            Try
                Dim dt As DataTable = cls<OwnerU>_<TableU>Data.Search(cmbFields.Text, cmbCondition.Text, txtSearch.Text)
                With Grid
                    .ItemsSource = dt.DefaultView
                    .CanUserAddRows = False
                    .CanUserDeleteRows = False
                    .IsReadOnly = True
                    If .Items.Count > 0 Then
                        .SelectedItem = .Items(0)
                        .ScrollIntoView(.SelectedItem)
                    End If
                End With
            Catch ex As System.Exception
                MsgBox("An error occurred in butSearch_Click..." & vbCrLf & ex.Message)
            Finally
            End Try
        End Using
    End Sub

End Class
